<?php
/**
 * Competitor list
 */

use Fox\MaterialManagement\Material;
use Fox\Html\Input;
use Fox\Grid\FieldCat\Field;
use Fox\SalesDistribution\Sales\Inquiry;
use Fox\SalesDistribution\Sales;
use Fox\Common\Localization\SubRegion;
$grid = new \Fox\Grid\Tree("gItems");
$grid->fieldcat(array(array("mat_hierarchy","hierarchy"),
						"label",
						"amount_initial",
						"amount_revised",
						array("amount_final","amount"),"gap","materialid","parentid"));

$grid->setCoche(true);
$grid->setInput(true,true,true,false);
$grid->setFieldAttr(array("materialid","label","parentid","hierarchy","gap"),"input",false);
$grid->setFieldAttr(array("materialid","label","parentid","hierarchy","gap"),"SetHiddenInput",true);
$grid->fieldcat()->field("materialid")->SetHidden(true);
$grid->fieldcat()->field("parentid")->SetHidden(true);
$grid->fieldcat()->field("hierarchy")->label(Msg(Material::BUNLDE_NAME,24));
$grid->fieldcat()->field("label")->label(Msg(Material::BUNLDE_NAME,26));
$label = $grid->fieldcat()->field("amount_initial")->label;
$grid->fieldcat()->field("amount_initial")->label($label." <em>*</em> (Dhs HT)");
$label = $grid->fieldcat()->field("amount_revised")->label;
$grid->fieldcat()->field("amount_revised")->label("$label (Dhs HT)");
$label = $grid->fieldcat()->field("amount")->label;
$grid->fieldcat()->field("amount")->label("$label (Dhs HT)");
//$grid->fieldcat()->setFieldAttr(array('amount_initial','amount_revised','amount'),
//					'cexit',Field::OUTPUT_NO_EMPTY);
$grid->fieldcat()->setFieldAttr(array('amount_initial','amount_revised','amount'),'SetHiddenInput',true);
$grid->fieldcat()->setFieldAttr(array('amount_initial','amount_revised','amount'),'align','right');
$grid->fieldcat()->setFieldAttr(array('amount_initial','amount_revised','amount'),'inputSize',Input::SIZE_SMALL);
$grid->fieldcat()->field("hierarchy")->width(1);

$grid->fieldcat()->setChangeableRule(Fox\Generator::useFunction(function ($data,$key){
			if(isset($data['_node']))
			{
				if($data['_node']->getChildrenCount() > 0)
				{
					return false;
				}
			}
			return true;
		}));
$grid->fieldcat()->field("gap")->setGenerator(Fox\Generator::useFunction(function ($data,$key){
		if(isset($data['amount']) && is_numeric($data['amount']))
		{
			if(isset($data['amount_revised'])  && $data['amount_revised'] > 0 ) 
			{
				$amount  =  ( ($data['amount_revised'] - $data['amount']) / $data['amount_revised'] ) * 100 ;
				return round($amount,1)." %";
			}
			elseif(isset($data['amount_initial']) && $data['amount_initial'] > 0  )
			{
				$amount  =  ( ($data['amount_initial'] - $data['amount']) / $data['amount_initial'] ) * 100 ;
				return round($amount,1)." %";
			}
			
		}
		return null;
	}));
	$grid->fieldcat()->field("gap")->setTotalGenerator(Fox\Generator::useFunction(function ($data,$key){
		if(isset($data['amount']) && is_numeric($data['amount']))
		{
			if(isset($data['amount_revised'])  && $data['amount_revised'] > 0 ) 
			{
				$amount  =  ( ($data['amount_revised'] - $data['amount']) / $data['amount_revised'] ) * 100 ;
				return round($amount,1)." %";
			}
			elseif(isset($data['amount_initial']) && $data['amount_initial'] > 0  )
			{
				$amount  =  ( ($data['amount_initial'] - $data['amount']) / $data['amount_initial'] ) * 100 ;
				return round($amount,1)." %";
			}
			
		}
		return null;
	}));
	
$grid->setTotal();
$grid->footer()->item(Fox\Grid::BUTTON_DELETE_LINE)->label("Supprimer");
$grid->footer()->add(new Fox\Html\Button("add","FoxDisplay('addMaterials','?mod=inquiry&act=materials&sac=add',null,'InputSerialize(\'eInquiry\')')"));

$block = new Fox\Html\Block("bItems",Msg(Sales::BUNDLE_NAME,28));
// Customized Layout
$inquiryid = Xview::instance()->val("inquiryid");
if(empty($inquiryid))	// Creation Mode
{
	$grid->fieldcat()->field("amount_revised")->SetHidden(true);
	$grid->fieldcat()->field("amount")->SetHidden(true);
	$grid->fieldcat()->field("gap")->SetHidden(true);
}
else
{
	// Depending on the status
	$status = Xview::instance()->val("status");
	if(empty($status) && isset($params['status']))
		$status = $params['status'];
	if($status == Inquiry::STATUS_WIN || $status == Inquiry::STATUS_LOST)
	{
		if($status == Inquiry::STATUS_LOST)
			$block->add(new Input(array("amount_final","amount")));
		else 
		{
			$grid->fieldcat()->field("amount")->label($label." <em>*</em> (Dhs HT)");
		}
	}
	else 
	{
		$grid->fieldcat()->field("amount")->SetHidden(true);
		$grid->fieldcat()->field("gap")->SetHidden(true);		
	}
}

$block->add($grid);
return $block;